function [SAR,turnPoints,longSort]= parabolicSAR( high,low,AFstart,AFmax,AFdelta)

% $License: BSD (use/copy/change/redistribute on own risk, mention the
% author) $
% History:
% 001: 01-May-2006 ,Natanel Eizenberg, First version.
% 002: 04-Nov-2012, Natanel Eizenberg, prepare for file exchange

% all values should be lined
if size(high,1)~=1; high=high'; end;
if size(low,1)~=1; low=low'; end;

type=1; %type=1 - high(i) & low(i), type=2 - maxHigh & minLow
% %filter high and low
% high=conv(high,[1 1 1 1 1 1])/6;
% high=high(5:161);
% low=conv(low,[1 1 1 1 1 1])/6;
% low=low(5:161);

% defults AFstart,  AFmax, AFdelta
if nargin==2
    AFstart=0.02;
    AFmax=0.2;
    AFdelta=0.02;
elseif nargin==3
    AFmax=0.2;
    AFdelta=0.02;
elseif nargin==4
    AFdelta=0.02;
end;

%memory allocation
SAR=zeros(size(high));
longSort=zeros(size(high));
turnPoints=zeros(size(high)); j=1;

% reset AF
AF=AFstart;

% SIP (SAR Initial Point) and first SAR
if high(1)<high(2)
    position='long';
    SAR(1)=min(low(1),low(2));
    maxHigh=max(high(1),high(2));
else
    position='sort';
    SAR(1)=max(high(1),high(2));
    minLow=min(low(1),low(2));
end;

% main loop
for i=2:length(high)

    % update AF
    if (AF<AFmax)
        if (strcmp(position,'long') && high(i)>maxHigh)
            maxHigh=high(i);
            AF=AF+AFdelta;
        elseif (strcmp(position,'sort') && low(i)<minLow)
            minLow=low(i);
            AF=AF+AFdelta;
        end;
    end;

    % new SAR
    if strcmp(position,'long')
        if type==1
            SAR(i)=AF*(high(i)-SAR(i-1))+SAR(i-1);
        else
            SAR(i)=AF*(maxHigh-SAR(i-1))+SAR(i-1);
        end;
        longSort(i)=1;
    else %strcmp(position,'sort')
        if type==1
            SAR(i)=AF*(low(i)-SAR(i-1))+SAR(i-1);
        else
            SAR(i)=AF*(minLow-SAR(i-1))+SAR(i-1);
        end
        longSort(i)=-1;
    end;


    % check for turning point
    if strcmp(position,'long')
        if (SAR(i)>low(i) || SAR(i)>low(i-1))
            AF=AFstart; %reset AF
            minLow=min(low(i),low(i-1)); % new minmum low
            SARt=max(high(i),high(i-1)); %use SARt as last day SAR
            if type==1
                SAR(i)=AF*(low(i)-SARt)+SARt; % new SAR as sort
            else
                SAR(i)=AF*(minLow-SARt)+SARt; % new SAR as sort
            end
            position='sort';
            turnPoints(j)=i; j=j+1;
            longSort(i)=-1;
        end;
    else %if strcmp(position,'sort')
        if (SAR(i)<high(i) | SAR(i)<high(i-1))
            AF=AFstart;
            maxHigh=max(high(i),high(i-1)); % new maximum high
            SARt=min(low(i),low(i-1)); %use SARt as last day SAR
            if type==1
                SAR(i)=AF*(high(i)-SARt)+SARt; % new SAR as long
            else
                SAR(i)=AF*(maxHigh-SARt)+SARt; % new SAR as long
            end;
            position='long';
            turnPoints(j)=i; j=j+1;
            longSort(i)=1;
        end;
    end;

end;% main loop

% set first longSort
longSort(1)=longSort(2);

% set last point as turn point
if j==1 | turnPoints(j-1)~=length(high)
    turnPoints(j)=length(high);
end;

% clear zeros
turnPoints=turnPoints(turnPoints~=0);